//
//  RentMapViewController.m
//  ZhuanZuBao
//
//  Created by 戴伟 on 2020/9/10.
//  Copyright © 2020 戴伟. All rights reserved.
//

#import "RentMapViewController.h"
#import "CustomAnnotationView.h"

@interface RentMapViewController ()<MAMapViewDelegate, AMapNaviCompositeManagerDelegate>

@property (nonatomic, assign) CGFloat lat;
@property (nonatomic, assign) CGFloat lon;

@property (nonatomic, strong) MAMapView *mapView;
@end

@implementation RentMapViewController

-(instancetype)initWithLat:(CGFloat)lat lon:(CGFloat)lon{
    if (self = [super init]) {
        _lat = lat;
        _lon = lon;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"地图";
    MAMapView *mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_SCREEN_WIDTH, DEVICE_SCREEN_HEIGHT-SafeAreaTopHeight)];
    _mapView = mapView;
    [self.view addSubview:mapView];
    mapView.delegate = self;
    [mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    mapView.showsUserLocation = YES;
    mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    
    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = CLLocationCoordinate2DMake(_lat, _lon);
    pointAnnotation.title = _community;
    pointAnnotation.subtitle = _metro;

    [mapView addAnnotation:pointAnnotation];
    [mapView setCenterCoordinate:pointAnnotation.coordinate animated:YES];
    
    UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:@"导航" style:UIBarButtonItemStylePlain target:self action:@selector(navi)];
    self.navigationItem.rightBarButtonItem = btn;
}


- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;
    }
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        // 设置为NO，用以调用自定义的calloutView
        annotationView.canShowCallout = NO;
        annotationView.selected = YES;
        // 设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;
}

-(void)navi{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"导航" message:@"选择导航方式" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
            [MBProgressHUD showError:@"未安装高德地图" toView:nil];
            return;
        }
        NSString *urlString = [[NSString stringWithFormat:@"iosamap://path?sourceApplication=%@&sid=BGVIS1&slat=%f&slon=%f&sname=当前位置&did=BGVIS2&dlat=%f&dlon=%f&dname=%@&dev=0&t=0",@"转租宝",_mapView.userLocation.location.coordinate.latitude, _mapView.userLocation.location.coordinate.longitude, _lat, _lon, _community] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
            [MBProgressHUD showError:@"未安装百度地图" toView:nil];
            return;
        }
        NSString *urlString =[[NSString stringWithFormat:@"baidumap://map/direction?origin={{我的位置}}&destination=latlng:%f,%f|name=%@&mode=driving&coord_type=gcj02",_lat, _lon, _community] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }];
    UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"Apple地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSString *urlString=[NSString stringWithFormat:@"http://maps.apple.com/?saddr=%f,%f&daddr=%f,%f",_mapView.userLocation.location.coordinate.latitude, _mapView.userLocation.location.coordinate.longitude,_lat, _lon ];
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [alert addAction:action3];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
//    self.compositeManager = [[AMapNaviCompositeManager alloc] init];
//    // 如果需要使用AMapNaviCompositeManagerDelegate的相关回调（如自定义语音、获取实时位置等），需要设置delegate
//    self.compositeManager.delegate = self;
//    AMapNaviCompositeUserConfig *config = [[AMapNaviCompositeUserConfig alloc] init];
//    [config setRoutePlanPOIType:AMapNaviRoutePlanPOITypeEnd location:[AMapNaviPoint locationWithLatitude:_lat longitude:_lon] name:_community POIId:nil];
//    [config setThemeType:AMapNaviCompositeThemeTypeLight];
//    [self.compositeManager presentRoutePlanViewControllerWithOptions:config];
}

@end
